package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceFunction;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceMenu;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.SysResourceProject;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceFunctionRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceMenuRepository;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.SysResourceProjectRepository;
import com.wgchao.identityrightsmanagement.query.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ResourcesQuery
 * @author: wgchao
 * @createTime: 2020/6/18 5:38 PM
 * @Description: TODO
 */
@Service
public class ResourcesQuery {
    @Autowired
    private SysResourceProjectRepository sysResourceProjectRepository;
    @Autowired
    private ProjectVoConvert projectVoConvert;
    @Autowired
    private SysResourceMenuRepository sysResourceMenuRepository;
    @Autowired
    private MenuDetailVoConvert menuDetailVoConvert;
    @Autowired
    private SysResourceFunctionRepository sysResourceFunctionRepository;
    @Autowired
    private FunctionVoConvert functionVoConvert;



    public ProjectVo findByMark(String proMark){
        SysResourceProject sysResourceProject = sysResourceProjectRepository.findFirstByProMark(proMark);
        if(sysResourceProject == null){
            return null;
        }
        return projectVoConvert.convert(sysResourceProject);
    }

    public List<ProjectVo> findAllProject(){
        List<SysResourceProject> sysResourceProjects = sysResourceProjectRepository.findAllByDisabledIsFalse();
        if(sysResourceProjects!=null && !sysResourceProjects.isEmpty()){
            List<ProjectVo> projectVos = sysResourceProjects.stream().map(sysResourceProject -> {
                return projectVoConvert.convert(sysResourceProject);
            }).collect(Collectors.toList());
            return projectVos;
        }
        return null;
    }

    /**
     * 查询项目菜单详情
     */
    public List<MenuDetailVo> findMenuDetailsOfPro(String proMark){
        //查询项目下顶级菜单
        List<SysResourceMenu> sysResourceMenus_super = sysResourceMenuRepository.findAllByProMarkAndSuperiorMarkIsNull(proMark);
        if(sysResourceMenus_super==null || sysResourceMenus_super.isEmpty()){
            return null;
        }
        List<MenuDetailVo> menuDetailVos = new ArrayList<>();
        for(SysResourceMenu sysResourceMenu : sysResourceMenus_super){
            MenuDetailVo menuDetailVo = menuDetailVoConvert.convert(sysResourceMenu);
            //查询菜单功能
            List<SysResourceFunction> sysResourceFunctions = sysResourceFunctionRepository
                    .findAllByMenuMark(sysResourceMenu.getMenuMark());
            if(sysResourceFunctions!=null && !sysResourceFunctions.isEmpty()){
                List<FunctionVo> functionVos = sysResourceFunctions.stream().map(sysResourceFunction -> {
                    return functionVoConvert.convert(sysResourceFunction);
                }).collect(Collectors.toList());
                menuDetailVo.setFunctionVos(functionVos);
            }
            //查询子菜单
            menuDetailVo.setChilds(findMenuDetailsOfChild(proMark, sysResourceMenu.getMenuMark()));

            menuDetailVos.add(menuDetailVo);
        }
        return menuDetailVos;
     }

     private List<MenuDetailVo> findMenuDetailsOfChild(String proMark, String menuMark){
         List<SysResourceMenu> sysResourceMenus_child = sysResourceMenuRepository
                 .findAllByProMarkAndSuperiorMark(proMark, menuMark);
         if(sysResourceMenus_child==null || sysResourceMenus_child.isEmpty()){
             return null;
         }
         List<MenuDetailVo> menuDetailVos = new ArrayList<>();
         for(SysResourceMenu sysResourceMenu : sysResourceMenus_child){
             MenuDetailVo menuDetailVo = menuDetailVoConvert.convert(sysResourceMenu);
             //查询菜单功能
             List<SysResourceFunction> sysResourceFunctions = sysResourceFunctionRepository
                     .findAllByMenuMark(menuMark);
             if(sysResourceFunctions!=null && !sysResourceFunctions.isEmpty()){
                 List<FunctionVo> functionVos = sysResourceFunctions.stream().map(sysResourceFunction -> {
                    return functionVoConvert.convert(sysResourceFunction);
                 }).collect(Collectors.toList());
                 menuDetailVo.setFunctionVos(functionVos);
             }
             //查询子菜单
             menuDetailVo.setChilds(findMenuDetailsOfChild(proMark, sysResourceMenu.getMenuMark()));

             menuDetailVos.add(menuDetailVo);
         }
         return menuDetailVos;
     }
}
